<?php
class SpageAction extends BaseAction{
	
	  
    //添加单页内容
    public function addSpage(){
    	$cdata = D('Cat')->getTypeCats('Spage');
    	$sdata = M('Spage')->field('cat')->findAll(); //单页内容
    	$tempcat = array();
    	if(count($sdata)>0){
    		foreach($cdata as $cat){
	    		$temp = true;
	    		foreach($sdata as $sd){
	    			if($sd['cat']==$cat['id']){
	    				$temp = false;
	    			}
	    		}
	    		if($temp){
	    			array_push($tempcat,$cat);
	    		}
    		}
    	}else{
    		$tempcat = $cdata;
    	}
    	
    	if(count($tempcat)<=0){
    		$value = '没有可添加的单页栏目，请先 <a href="'.__URL__.'/catList">添加单页栏目</a>'; 
    		if(count($cdata)>0){
    			 $value .= ' 或是 <a href="'.__URL__.'/contentList/type/Spage">修改单页内容</a>';
    		}
    		$value .= '，然后进行添加单页内容操作！';
    		header("Content-type: text/html; charset=utf-8");
    		echo "<div style='border:2px solid green; background:#f1f1f1; padding:20px;margin:20px;width:800px;font-weight:bold;color:green;text-align:center;'>".$value." </div> <br /><br />";
    	}else{
    		$model = D('Model')->getModelByTable('Spage');
    		$this->assign("width",$tempcat[0]['maxwidth']."px");
    		$this->assign("cats",$tempcat);
    		$this->display();
    	}
    }
    
    //修改单页
    public function editSpage(){
    	
    	$spage = M('Spage')->find($_GET['id']);
    	$cat   = D('Cat')->getCatById($spage['cat']);
    	$model = D('Model')->getModelByTable('Spage');
    	$this->assign('spage',$spage);
    	$this->assign('cat',$cat);
    	$this->assign('width',$model['maxwidth']."px");
    	$this->display();
    }
    //执行单页修改操作
    public function doEditSpage(){
    	$spage = D('Spage');
    	if($spage->create()){
					$model = D('Model')->getModelByTable('Spage');
					$info = upload('content',$model['cwidth'],$model['cheight']); //上传分类图片
					if($info){
						$spage->img = UPLOAD_URL.'content/thumb_'.$info[0]['savename'];
				  }
					if($spage->save()){
    		 		$this->assign("jumpUrl",__APP__."/Content/contentList/type/Spage");
    		 		$this->success('修改单页内容成功！');
	    		 }else{
	    		 	  $this->error('修改单页内容失败！');
	    		 }
			}else{
					$this->error($spage->getError());
					return false;
			}    	
    }
    //执行单页内容添加操作
    public function doAddSpage(){
    	
    	$spage = D('Spage');
    	if($spage->create()){
					$spage->ctime = time(); //发布时间 
					$spage->uid   = $_SESSION['adminId'];      //发布者ID
					$spage->uname = $_SESSION['account'];      //发布者账号
					$spage->status = 0;                        //状态默认为0
					$spage->content = showHtml($_POST['content']);
					$model = D('Model')->getModelByTable('Spage');
					$info = upload('content',$model['cwidth'],$model['cheight']); //上传分类图片
					if($info){
						$spage->img = UPLOAD_URL.'content/thumb_'.$info[0]['savename'];
				  }
					if($spage->add()){
    		 		$this->assign("jumpUrl",__APP__."/Content/contentList/type/Spage");
    		 		$this->success('添加单页内容成功！');
	    		 }else{
	    		 	  $this->error('添加单页内容失败！');
	    		 }
			}else{
					$this->error($spage->getError());
					return false;
			}
			
    	
    	
    }
    
    //审核多条记录
   public function doAudit()
	 {
			//删除指定记录
			$model = M('Spage');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->setField('status',1)){
						$this->assign("jumpUrl", __APP__."/Content/contentList/type/Spage");
						$this->success('操作成功！');
					}else {
						$this->error("操作失败!");
					}
				}else {
					$this->error('非法操作');
				}
			}
	 }

    //删除多条记录
   public function delete()
	 {
			//删除指定记录
			$model = M('Spage');
			if(!empty($model)) {
				$id = $_REQUEST['id'];
				if(isset($id)) {
					if($model->where('id In('.$id.')')->delete()){
						$this->assign("jumpUrl",__APP__."/Content/contentList/type/Spage");
						$this->success('删除成功！');
						

					}else {
						$this->error("删除失败!");
					}
				}else {
					$this->error('非法操作');
				}
				$this->forward();
			}
	 }
   public function del(){
   	  $adObj = M('Spage');
   	  $ad = $adObj->find($_GET['id']);
   	  if($ad){
   	  		$adx = $adObj->where('id='.$_GET['id'])->delete();
   	  }else{
   	  	
   	  	$this->error('您要删除的对象不存在！');
   	  }
   		
   		if($adx){
   			$this->success('删除成功！');
   		}else{
   			$this->error('删除失败！');
   		}
   }
   
    
 
}
?>